package com.baroxtech.testanoterecorder;

import android.content.Intent;
import android.os.Bundle;

import com.baroxtech.anoterecorder.ABLEMIDIScanningActivity;
import com.baroxtech.anoterecorder.AMIDIDev;
import com.baroxtech.anoterecorder.AMIDIDevManager;
import com.baroxtech.anoterecorder.ANote;
import com.baroxtech.anoterecorder.ANoteRecorder;
import com.baroxtech.anoterecorder.AMIDICommand;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private ANoteRecorder noteRecorder;
    private StringAdapter stringAdapter = new StringAdapter();
    private RecyclerView recyclerView;
    private AMIDIDevManager.EventHandler eventHandler = new AMIDIDevManager.EventHandler() {
        @Override
        public void onEvent(int eventType, final List<AMIDICommand> commands, final AMIDIDev dev) {
            switch (eventType) {
                case AMIDIDevManager.EventType.MIDI_COMMANDS:
                    noteRecorder.process(commands);
                    break;
                case AMIDIDevManager.EventType.CONNECTED:
                    Toast.makeText(MainActivity.this, "已连接 " + dev.getName(), Toast.LENGTH_SHORT).show();
                    if (noteRecorder.getState() == ANoteRecorder.State.PAUSING) {
                        noteRecorder.setState(ANoteRecorder.State.RECORDING);
                    }
                    break;
                case AMIDIDevManager.EventType.DISCONNECTED:
                    Toast.makeText(MainActivity.this, "已断开 " + dev.getName(), Toast.LENGTH_SHORT).show();
                    noteRecorder.setState(ANoteRecorder.State.PAUSING);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        noteRecorder = ANoteRecorder.create(0.064, new ANoteRecorder.EventHandler() {
            @Override
            public void onEvent(double packTime, final List<ANote> notes) {
                for (ANote note : notes) {
                    String s = String.format("ts: %.3f, pitch: %d, vel: %d, channel: %d", note.onset, note.pitch, note.velocity, note.channel);
                    Log.i(TAG, s);
                    stringAdapter.add(s);
                    recyclerView.scrollToPosition(stringAdapter.getItemCount() - 1);
                }
            }
        });
        noteRecorder.setRecordNoteOff(false);

        AMIDIDevManager.getInstance().init(getApplication());
        AMIDIDevManager.getInstance().addEventHandler(eventHandler);

        initView();

        AMIDIDevManager.getInstance().resume();
    }

    private void initView() {
        // 显示蓝牙控制列表
        findViewById(R.id.btn_ble).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, ABLEMIDIScanningActivity.class);
                startActivity(intent);
            }
        });
        // 控制录制
        ((CompoundButton)findViewById(R.id.switch_recorder)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    noteRecorder.setState(ANoteRecorder.State.RECORDING);
                } else {
                    noteRecorder.setState(ANoteRecorder.State.STOPPED);
                }
            }
        });
        // midi 数据显示
        recyclerView = (RecyclerView)findViewById(R.id.recycler_midi_data);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); // 添加分割线
        recyclerView.setAdapter(stringAdapter);
    }

    @Override
    protected void onDestroy() {
        AMIDIDevManager.getInstance().removeEventHandler(eventHandler);
        super.onDestroy();
    }
}
